// pages/index/index.js
var network = require("../../utils/network.js");
var page = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    circular: true,
    indicatordots: "true",
    autoplay: "true",
    interval: "3000",
    duration: "500",
    swiperCurrent: 0,
    scrollTop: 0,
    scrollHeight: 0,
    banner: [],
    list: [
      { 
        id: 1,
        imgUrl: '../../images/icon01.jpg',
        title: '健康管理',
        content: 'HEALTH MANAGEMENT'
      },
      { 
        id: 2,
        imgUrl: '../../images/icon02.jpg',
        title: '装机备案',
        content: 'USER REGISTRATION'
      },
      { 
        id: 3,
        imgUrl: '../../images/icon03.jpg',
        title: '售后申请',
        content: 'AFTER-SALE SERVICE'
      }
    ],
    Recommends: []
  },
  /**
   * 轮播图
  */ 
  banner: function(){
    let that = this;
    let URL = network.server() + 'banner/info';
    let Params = {};
    let Method = 'GET';
    let Message = '正在加载数据';
    network.requestPost(URL, Params, Method, Message, function (res) {
      console.log('轮播图---', res)
      if (res.code == 0) {
        let banner = res.data;
        that.setData({
          banner: banner
        })
      }
    }, function () {})
  },
  /**
   * 健康管理
  */
  menu: function(e){
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    var id = that.data.list[index].id;
    var title = that.data.list[index].title;
    if (id == 1){
      wx.navigateTo({
        url: '../healthControl/index?title=' + title,
      })
    }
    else if(id == 2){
      wx.navigateTo({
        url: '../register/index?title=' + title,
      })
    }
    else if (id == 3) {
      wx.navigateTo({
        url: '../afterService/index?title=' + title,
      })
    }  
  },
  /**
   * 推荐阅读
  */
  Recommend: function () {
    let that = this;
    let page = 0;
    let pagesize = 100;
    let URL = network.server() + 'cloud-dynamic/list';
    let Params = { page: page, pagesize: pagesize };
    let Method = 'GET';
    let Message = '';
    network.requestLoading(URL, Params, Method, Message, function (res) {
      console.log('推荐阅读---', res)
      if (res.code == 0) {
        let lists = res.data.list;
        that.setData({
          Recommends: lists,
        })
      }
    }, function () {})
  },
  /**
   *详情页
  */
  detail: function(e){
    //let index = parseInt(e.currentTarget.dataset.index);
    //let id = this.data.list[index].id;
    let id = parseInt(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: '../ortherDetail/detail?id=' + id,
    })
  },

  /**
   *上拉加载  
  */
  bindDownLoad: function () {
    // let that = this;
    // // let page = 1;
    // let pagesize = 1;
    // let URL = 'http://api.tbyl.moqilin.net/cloud-dynamic/list';
    // let Params = { page: page, pagesize: pagesize };
    // let Method = 'GET';
    // let Message = '正在加载数据';
    // network.requestLoading(URL, Params, Method, Message, function (res) {
    //   console.log('推荐阅读---', res)
    //   if (res.code == 0) {
    //     let Recommend = that.data.Recommends;
    //     for (var i = 0; i < res.data.list.length; i++) {
    //       Recommend.push(res.data.list[i]);
    //     }
    //     that.setData({
    //       Recommends: Recommend,
    //     })
    //     page ++;
    //     console.log(page)
    //   }
    // }, function () {
    //   wx.showToast({
    //     title: '加载数据失败',
    //   })
    // })
  },
  scroll: function (event) {
    //   该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    // this.setData({
    //   scrollTop: event.detail.scrollTop
    // });
  },
  /**
   * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    //   这里要非常注意，微信的scroll-view必须要设置高度才能监听滚动事件，所以，需要在页面的onLoad事件中给scroll-view的高度赋值
    var that = this;
    // wx.getSystemInfo({
    //   success: function (res) {
    //     console.info(res.windowHeight);
    //     that.setData({
    //       scrollHeight: res.windowHeight
    //     });
    //   }
    // });
    that.banner()
  },
  onShow: function (){
    this.Recommend(); 
  },

  onShareAppMessage: function () { }
})